<?php
/**
 * SystemEmailController is the default controller for this application
 */
class EmailController extends ControllerBase {
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'mailing' );
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			try {
				// get post values
				$type = $this->getPost ( 'type', null );
				$subject = $this->getPost ( 'subject', null );
				$message = $this->getPost ( 'message', null );
				
				if (! is_null ( $type ) && ! empty ( $subject ) && ! empty ( $message )) {
					$userDataService = new UsersDataService ();
					
					// where array
					$criterias = array ("status" => UsersEntity::ACTIVE, "hasVerified" => 1 );
					
					// type
					switch ($type) {
						// all
						case ALL_PACKAGE :
							$whereStr = NULL;
							break;
						
						// Package Option 1
						case PACK_OPTION_1 :
							$whereStr = "
									packageOptionId = '" . PACK_OPTION_1 . "'
							";
							break;
						
						// Package Option 2
						case PACK_OPTION_2 :
							$whereStr = "
									packageOptionId = '" . PACK_OPTION_2 . "'
							";
							break;
						
						// Package Option 3
						case PACK_OPTION_3 :
							$whereStr = "
									packageOptionId = '" . PACK_OPTION_3 . "'
							";
							break;
						// Package Option 4
						case PACK_OPTION_4 :
							$whereStr = "
									packageOptionId = '" . PACK_OPTION_4 . "'
							";
							break;
					}
					
					// admin email
					$settingsDataService = new SettingsDataService ();
					
					// Getting settings from DB
					$adminEmail = $settingsDataService->getValue ( SETTINGS_ADMIN_EMAIL );
					$adminName = $settingsDataService->getValue ( SETTINGS_ADMIN_NAME );
					
					if (empty ( $whereStr )) {
						$userEntities = $userDataService->getItems ( $criterias );
					} else {
						$userEntities = $userDataService->getItems ( $criterias, $whereStr );
					}
					$mailer = new MyMailUtils ();
					if (is_array ( $userEntities )) {
						foreach ( $userEntities as $userEntity ) {
							// replace variables
							$replacedSubject = str_replace ( '[name]', $userEntity->firstName . " " . $userEntity->lastName, $subject );
							$replacedContent = str_replace ( '[name]', $userEntity->firstName . " " . $userEntity->lastName, $message );
							
							// send mail					
							$mailer->addFrom ( $adminEmail, $adminName );
							
							$mailer->addTo ( $userEntity->email, $userEntity->firstName . " " . $userEntity->lastName );
							$resultSendmail = $mailer->send ( $replacedSubject, $replacedContent );
							$mailer->resetAll ();
						
						}
					}
					$returnMsg = '<span style="color: #006400;">This message was sent to users!</span>';
				} else {
					$returnMsg = '<span style="color: #FF0000;">Please fill in all fields!</span>';
				}
			} catch ( Exception $ex ) {
				$returnMsg = '<span style="color: #FF0000;">Error occurs, please try again later!</span>';
			}
			$this->smarty->assign ( 'returnMsg', $returnMsg );
			$this->smarty->assign ( 'type', $type );
			$this->smarty->assign ( 'subject', $subject );
			$this->smarty->assign ( 'message', $message );
		}
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/Email/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}

}
